package com.servlet;

import com.util.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        int userNum= Integer.parseInt(request.getParameter("usernum"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        int status = Integer.parseInt(request.getParameter("status"));
        int number=0;
        Connection conn = DBUtil.getConnectDb();
        String sql="INSERT INTO admin(usernum,username,password,status) VALUES(?,?,?,?)";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, userNum);
            ps.setString(2, username);
            ps.setString(3, password);
            ps.setInt(4,status);
            number=ps.executeUpdate();
            System.out.println(number);
        } catch (SQLException e) {
            throw new RuntimeException(e);

        }
        if (number!=0){
            response.sendRedirect("login.jsp");
        }else {
            response.sendRedirect(".jsp");
        }
    }
}
